from flask import Blueprint
from flask import request, Response

from common.utils.ldapManager import MyLdap

ldapBP = Blueprint('ldap', __name__)


def reg_ldap_bp(app):
    app.register_blueprint(ldapBP)


@ldapBP.route('/login', methods=['POST'])
def ldapRun():
    """
    1.路由入口方法
    """
    myldap = MyLdap()
    import json
    if request.method == "GET":
        pass

    elif request.method == "POST":
        Data = request.get_json()
        username = Data.get('username', None)
        password = Data.get('password', None)
        if username and password:
            flag = myldap.ldap_auth(username, password)
            print(username, password, flag)
            if flag:
                return Response(json.dumps({"code": 0, "data": '登陆成功'}), mimetype='application/json')
            else:
                return Response(json.dumps({"code": 1, "data": '账号或密码错误！'}), mimetype='application/json')
        else:
            parameterInfo = "参数不足或错误,查询所有请输入all"
            return Response(json.dumps({"code": 1, "data": parameterInfo}), mimetype='application/json')
